<template>
  <div class="app-container">
    <el-form  :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="阶段：" prop="stage">
        <el-select v-model="queryParams.stage" placeholder="请选择阶段" clearable>
          <el-option
            v-for="dict in dict.type.sys_stage"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="授课内容" prop="courseContent">
          <el-input
            v-model="queryParams.courseContent"
            placeholder="请输入授课内容"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      <el-form-item  prop="date">
        <el-checkbox v-model="queryParams.date">今天之后的课表</el-checkbox>
      </el-form-item>
      <el-form-item prop="day">
        <el-checkbox v-model="queryParams.day">只看学习日</el-checkbox>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="planList" @selection-change="handleSelectionChange" :row-class-name="tableRowClassName">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="日期" align="center" prop="date" fixed="left">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="班级名字" align="center" prop="className" />
      <el-table-column label="阶段" align="center" prop="stage" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_stage" :value="scope.row.stage"/>
        </template>
      </el-table-column>
      <el-table-column label="周几" align="center" prop="weekday">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ya_weekday" :value="scope.row.weekday"/>
        </template>
      </el-table-column>
      <el-table-column label="第几天" align="center" prop="day" />
      <el-table-column label="授课内容" align="center" prop="courseContent" />
      <el-table-column label="备注" align="center" prop="remarks" />
    </el-table>

    <pagination
      v-show="total>0"
      :page-sizes="[200,500]"
      :page-size="200"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>
<style>
.el-table .warning-row {
  background: #34bfa3;
}

.el-table .success-row {
  background: #f0f9eb;
}

.el-table .yellow-row {
  background: #e6a700;
}
</style>
<script>
import {clearConfig,addWorkday,addHoliday,previewTeachingPlan,listAllPlan,listPlanByStudent, getPlan, delPlan, addPlan, updatePlan } from "@/api/teaching/teaching_plan";
import {listAllClasses} from "@/api/teaching/classes";

export default {
  name: "Plan",
  dicts: ['sys_stage','ya_attendance_status','ya_weekday','ya_task_status','ya_task_qualified'],
  data() {
    return {
      drawer: false,
      direction: 'ltr',
      interviewList:[],
      baseUrl: process.env.VUE_APP_BASE_API,
      planId:null,
      completionList:[],
      completionOpen:false,
      completionLoading:false,
      completionForm: {},
      //学员考勤信息
      studentAttendanceList:[],
      classesList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教学计划表格数据
      planList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      tracking_open:false,
      attendance_open:false,
      trackingForm:{},
      attendanceForm:{},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 180,
        date: null,
        day: null,
        courseContent: null,
        className: null,
        stage: null
      },
      // 表单参数
      form: {
        className: undefined,
        stage: undefined,
        beginDate:""
      },
      interviewForm: {
      },
      // 表单校验
      rules: {
        date: [
          { required: true, message: "日期不能为空", trigger: "blur" }
        ],
        day: [
          { required: true, message: "第几天不能为空", trigger: "blur" }
        ],
        courseContent: [
          { required: true, message: "授课内容不能为空", trigger: "blur" }
        ],
        remarks: [
          { required: true, message: "备注事项不能为空", trigger: "blur" }
        ],
        className: [
          { required: true, message: "班级名字不能为空", trigger: "blur" }
        ],
        class_id: [{
          required: true,
          message: '请选择班级',
          trigger: 'change'
        }],
        stage: [{
          required: true,
          message: '请选择阶段',
          trigger: 'change'
        }],
        beginDate: [{
          required: true,
          message: '请选择起始日期',
          trigger: 'change'
        }]
      },
      generationDisabled:true,
      planAllList:[]
    };
  },
  created() {
    this.getList();
    this.getClassesList();
  },
  methods: {
    handleClose(done) {
      done();
    },
    // checkInterview(row){
    //   queryDailyInterviewListById(row.id).then(response=>{
    //     this.interviewList = response.rows;
    //     this.drawer = true;
    //   });
    // },
    preList(url){
      return url.split(",");
    },
    cancelCompletion(){
      this.completionOpen = false;
      this.resetDailyTask();
    },
    // updateQualified(row){
    //   let dailyTaskId = row.id;
    //   let qualified = row.qualified;
    //   updateQualified({dailyTaskId,qualified}).then(response => {
    //     this.$modal.msgSuccess("更新成功");
    //   }).catch(err=>{
    //     this.$modal.msgError("更新失败");
    //   });
    // },
    resetDailyTask() {
      this.completionForm = {
        date: null,
        status:null,
        studentName:null,
        classesId:null,
      };
      this.resetForm("completionForm");
    },
    // checkDailyTask(row) {
    //   if(row.date){
    //     this.resetDailyTask();
    //     this.completionForm.date = row.date
    //     this.completionForm.classesId = row.classesId
    //   }
    //   this.completionLoading = true;
    //   getCompletion(this.completionForm).then(response => {
    //     this.completionList = response.rows;
    //     this.completionOpen = true;
    //     this.completionLoading = false;
    //   });
    // },
    // submitAttendanceForm() {
    //   updateAttendance(this.studentAttendanceList,this.classesId).then(response => {
    //     this.$modal.msgSuccess("修改成功");
    //     this.attendance_open = false;
    //     this.getList();
    //   });
    // },
    // submitTrackingForm() {
    //   this.$refs["trackingForm"].validate(valid => {
    //     if (valid) {
    //       updateTeaching_tracking(this.trackingForm).then(response => {
    //         this.$modal.msgSuccess("修改成功");
    //         this.tracking_open = false;
    //         this.getList();
    //       });
    //     }
    //   });
    // },
    // handleAddTracking(row) {
    //   this.resetTrackingForm();
    //   this.title = "编辑教学跟踪信息";
    //   this.tracking_open = true;
    //
    //   getTeaching_tracking(row.date,row.classesId).then(response => {
    //     if (response.data) {
    //       this.trackingForm = response.data;
    //     }else {
    //       this.trackingForm.teachingPlanId=row.id;
    //       this.trackingForm.date=row.date;
    //       this.trackingForm.classesId=row.classesId;
    //     }
    //   })
    // },
    // handleAddAttendance(row) {
    //   this.resetTrackingForm();
    //   this.classesId = row.classesId;
    //   this.attendance_open = true;
    //   //查询出这个班所有学生
    //   listAttendance({"classesId":row.classesId,"date":row.date}).then(response => {
    //     //radio默认值是string，需要将int转string
    //     response.rows.forEach(stu=>{
    //       stu.attendanceStatus = stu.attendanceStatus + "";
    //       //设置默认考勤日期
    //       stu.date=row.date;
    //     })
    //     this.studentAttendanceList = response.rows;
    //   });
    // },
    // 表单重置
    resetTrackingForm() {
      this.trackingForm = {
        id : null,
        teachingPlanId: null,
        unfinishedHomeworkAndUnfeedbackList: null,
        leaveNumber: null,
        lateNumber: null,
        patrolAndCommunicationRecord: null,
        studentLearningStatus: null,
        studentAttentionProcessingPlan: null
      };
      this.resetForm("trackingForm");
    },
    // 表单重置
    resetAttendanceForm() {
      this.attendanceStatusList = [];
      this.resetForm("attendanceForm");
    },
    getClassesList() {
      listAllClasses().then(response => {
        this.classesList = response.rows;
      });
    },
    isHoliday(row){
      if (row.courseContent === '自习' || row.courseContent.includes('节') || row.courseContent.includes('休息')) {
        return true;
      }
      else {
        return false;
      }
    },
    tableRowClassName({row, rowIndex}) {
      if (row.courseContent === '自习' ) {
        return 'yellow-row';
      }else if(row.courseContent.includes('节') || row.courseContent.includes('休息')){
        return 'warning-row';
      }
      else {
        return 'success-row';
      }
      return '';
    },
    /** 查询教学计划列表 */
    getList() {
      this.loading = true;
      console.log(this.queryParams.day)
      if (this.queryParams.day === true){
        this.queryParams.day = 0;
      }
      if (this.queryParams.day === null){
        this.queryParams.day = -1;
      }
      console.log(this.queryParams.day)
      listPlanByStudent(this.queryParams).then(response => {
        console.log(response.data)
        this.planList = response.data.teachingPlanList;
        // this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancelTrackingForm() {
      this.tracking_open = false;
      this.resetTrackingForm();
    },
    cancelAttendanceForm() {
      this.attendance_open = false;
      this.resetAttendanceForm();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        date: null,
        day: null,
        courseContent: null,
        remarks: null,
        className: null,
        stage: null,
        beginDate: ""
      };
      this.resetForm("form");
      this.planAllList = [];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    // handleAddPlan() {
    //   this.reset();
    //   this.open = true;
    //   this.title = "添加教学计划";
    // },
    // /** 修改按钮操作 */
    // handleUpdate(row) {
    //   this.reset();
    //   const id = row.id || this.ids
    //   getPlan(id).then(response => {
    //     this.form = response.data;
    //     this.open = true;
    //     this.title = "修改教学计划";
    //   });
    // },
    /** 提交按钮 */
    // submitForm() {
    //   this.$refs["form"].validate(valid => {
    //     if (valid) {
    //       if (this.form.id != null) {
    //         updatePlan(this.form).then(response => {
    //           this.$modal.msgSuccess("修改成功");
    //           this.open = false;
    //           this.getList();
    //         });
    //       } else {
    //         addPlan(this.form).then(response => {
    //           this.$modal.msgSuccess("新增成功");
    //           this.open = false;
    //           this.getList();
    //         });
    //       }
    //     }
    //   });
    // },
    /** 删除按钮操作 */
    // handleDelete() {
    //   this.$refs["form"].validate(valid => {
    //     if (valid) {
    //       let v = this;
    //       this.$modal.confirm('删除后相关的数据不会有影响，可以删除后重新创建！是否确认该阶段删除教学计划？').then(function() {
    //         delPlan(v.form);
    //         v.planAllList = [];
    //       })
    //     }
    //   })

    // },
    // /** 导出按钮操作 */
    // handleExport() {
    //   this.download('teaching/plan/export/plan', {
    //     ...this.queryParams
    //   }, `plan_${new Date().getTime()}.xlsx`)
    // },
    onOpen() {},
    onClose() {
      this.$refs['form'].resetFields()
    },
    close() {
      this.$emit('update:visible', false)
    }
    // handleConfirm() {
    //   let data = {
    //     teachingPlanList : this.planAllList,
    //   };
    //   Object.assign(data,  this.form);
    //   console.log(data)
    //   addPlan(data).then(response => {
    //     this.$modal.msgSuccess("新增成功");
    //     this.open = false;
    //     this.getList();
    //   });
    // },
    // queryTeachingPlan(){
    //   this.$refs["form"].validate(valid => {
    //     if (valid) {
    //       listAllPlan(this.form).then(response => {
    //         this.planAllList = response.rows;
    //         if(response.rows.length==0){
    //           this.generationDisabled=false;
    //           this.$modal.msgSuccess("该班级还未创建教学计划！");
    //         }else{
    //           this.generationDisabled=true;
    //           //获取第一天计划日期
    //           this.form.beginDate = response.rows[0].date;
    //           this.$modal.msgWarning("该班级已经创建教学计划，请细心修改！");
    //         }
    //       });
    //     }})
    // },
    // clearConfig(){
    //   this.$refs["form"].validate(valid => {
    //     if (valid) {
    //       clearConfig(this.form).then(response => {
    //         this.planAllList = response.rows;
    //       });
    //     }})
    // },
    // previewTeachingPlan() {
    //   // if (!this.form.beginDate || this.form.beginDate == "") {
    //   //   this.$modal.msgWarning("请选择开课日期！");
    //   //   return;
    //   // }
    //   previewTeachingPlan(this.form).then(response => {
    //     this.planAllList = response.data;
    //     if(response.data.length==0){
    //       this.generationDisabled=false;
    //       this.$modal.msgSuccess("该班级还未创建教学计划！");
    //     }
    //   }).catch(error => {
    //     this.$modal.msgError(error.response.data.msg);
    //   });
    // },
    // addHoliday(i,row,type) {
    //   row.beginDate = this.form.beginDate;
    //   row.type = type;
    //   console.log(row.type);
    //   addHoliday(row).then(response => {
    //     this.planAllList = response.data;
    //     if(response.data.length==0){
    //       this.generationDisabled=false;
    //       this.$modal.msgSuccess("该班级还未创建教学计划！");
    //     }
    //   });
    // },
  //   addWorkday(i,row) {
  //     row.beginDate = this.form.beginDate;
  //     console.log(row)
  //     addWorkday(row).then(response => {
  //       this.planAllList = response.data;
  //       if(response.data.length==0){
  //         this.generationDisabled=false;
  //         this.$modal.msgSuccess("该班级还未创建教学计划！");
  //       }
  //     });
  //   },
  }
};
</script>
